# Contributor: Julien (jvoisin) Voisin <julien.voisin@dustri.org>
# Maintainer:
pkgname=matterhorn
pkgver=50200.12.0
pkgrel=0
pkgdesc="feature-rich Unix terminal client for the Mattermost chat system"
url="https://github.com/matterhorn-chat/matterhorn"
arch="x86_64" # Limited by GHC / Cabal
license="BSD-3-Clause"
makedepends="cabal bash ghc zlib-dev ncurses-dev"
subpackages="$pkgname-doc"
options="net"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/matterhorn-chat/matterhorn/archive/$pkgver.tar.gz
	cabal.config
"
_cabal_home="$srcdir/dist"

_cabal_update() {
	msg "Freezing $pkgname dependencies"

	# Resolve deps and generate fresh cabal.config with version constraints.
	HOME="$_cabal_home" cabal v1-update
	(
		cd "$builddir"
		HOME="$_cabal_home" cabal v1-freeze --shadow-installed-packages

		# Add version tag at the first line.
		sed -i "1i--$pkgver" "cabal.config"

		mv "cabal.config" "$startdir/"
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	ln -sf "$srcdir/cabal.config" "$builddir/cabal.config"
}

build() {
	HOME="$_cabal_home" cabal v1-update
	HOME="$_cabal_home" cabal v1-install \
		--disable-documentation \
		happy
	HOME="$_cabal_home" cabal v1-install \
		--disable-documentation \
		--only-dependencies
	HOME="$_cabal_home" cabal v1-configure \
		--prefix='/usr' \
		--bindir='$prefix/bin' \
		--docdir='$prefix/share/doc' \
		--datadir='$prefix/share' \
		--htmldir='$docdir/html' \
		--libdir='$prefix/lib' \
		--libsubdir="$pkgname" \
		--datasubdir="$pkgname" \
		--dynlibdir="$pkgname" \
		--sysconfdir='/etc' \
		--disable-library-profiling \
		--disable-profiling \
		--disable-shared \
		--enable-executable-stripping \
		--flags='GMP FFI standalone'
	HOME="$_cabal_home" cabal v1-build -j
}

package() {
	HOME="$_cabal_home" cabal v1-copy --destdir="$pkgdir"

	rm -r "$pkgdir/usr/lib/$pkgname"

	mkdir -p "$pkgdir/usr/share/doc/$pkgname"
	mv "$pkgdir/usr/share/doc/LICENSE" "$pkgdir/usr/share/doc/$pkgname"
}

sha512sums="38b3b828dbb0807065ae9ee53c9fee2a7ae2ce3357095b36df76478b66a917de8ebae9d3eddfa7743c6fbcb9c3df2a334397db12121007d13a042393c9a30e6c  matterhorn-50200.12.0.tar.gz
fd8ba9c8d4185a2ed0805b202b0d50b541cce956ee8e51943dd3c3846a5e85758d6a8dcf07a3b379587791a737a3edda85359c89ee8376ec51f56c45a07a3088  cabal.config"

